import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train.dart';
import 'package:flutter_learn_vi/src/page/common/common_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/train/model/train_model.dart';
import 'package:flutter_learn_vi/src/page/train/train_teacher_start_page.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/16.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///培训详情页面二
class TrainTabDetails2TeacherPage extends StatefulWidget {
  //培训ID
  final TrainApproveBean approveBean;

  const TrainTabDetails2TeacherPage({Key key, this.approveBean})
      : super(key: key);

  @override
  _TrainTabDetails2TeacherPageState createState() =>
      _TrainTabDetails2TeacherPageState();
}

class _TrainTabDetails2TeacherPageState
    extends State<TrainTabDetails2TeacherPage>
    with AutomaticKeepAliveClientMixin, TrainModel {
  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    getUserSelectTrain();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return SingleChildScrollView(
      child: Column(
        children: [
          Container(
            alignment: Alignment.center,
            width: double.infinity,
            height: 74,
            padding: EdgeInsets.only(top: 10),
            child: Text(
              "进度",
              style: TextStyle(fontSize: 18),
            ),
            color: Colors.white,
          ),
          TrainTeacherSelectCoursePage(
            approveBean: widget.approveBean,
            isScaffold: false,
          ),
          buildElevatedButton(),
          Container(
            padding: EdgeInsets.all(16),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text("小提示:"),
                SizedBox(
                  width: 14,
                ),
                Expanded(
                  child: Text(
                    "尊敬的教师您好！！！ 可点击完成培训，结束本次培训",
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.grey,
                    ),
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget buildElevatedButton() {
    if (_isLoading) {
      return LoadingWidget();
    }
    if (_userSelectTrainBena == null) {
      return Container();
    }
    //5 进行中 ；6已完成
    int trainStatus = _userSelectTrainBena.trainStatus;
    if (trainStatus == 6) {
      return ElevatedButton(
        style: ButtonStyle(
          backgroundColor: MaterialStateProperty.resolveWith((states) {
            //设置按下时的背景颜色
            //默认不使用背景颜色
            return Colors.grey;
          }),
        ),
        onPressed: () {},
        child: Text("已完成该培训"),
      );
    }
    LogUtils.e("trainStatus $trainStatus");
    return ElevatedButton(
      onPressed: () {
        teacherFinishTrain();
      },
      child: Text("点击完成培训22"),
    );
  }

  TrainBean _userSelectTrainBena;
  bool _isLoading = true;

  //查询用户参与培训 的详情
  void getUserSelectTrain() async {
    _userSelectTrainBena = await querySelectTrain(widget.approveBean.trainId);
    if (mounted) {
      setState(() {});
    }
    _isLoading = false;
  }

  //教师点击完成培训
  void teacherFinishTrain() {
    showCommonAlertDialog(
      context: context,
      contentMessag: "确定完成培训???",
      selectText: "确定",
      cancleText: "取消操作",
      selectCallBack: () async {
        ToastUtils.showToast("已完成了培训 可查看学员对课程的评价");
        await userFinishTrain(flag: 1, trainId: widget.approveBean.trainId);
        getUserSelectTrain();
      },
    );
  }
}
